<template>
  <div class="edit-container">
    <!-- 导航条 -->
    <van-nav-bar
      left-arrow
      title="编辑资料"
      @click-left="$router.back()"
    ></van-nav-bar>
    <!-- 头像部分 -->
    <div class="avatar">
      <!-- 上传头像文件 -->
      <van-image fit="cover" round :src="userInfo.photo" />
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 信息展示 -->
    <van-cell-group>
      <van-cell
        is-link
        title="名称"
        :value="userInfo.name"
        @click="editNameClick"
      />
      <van-cell
        is-link
        title="性别"
        :value="userInfo.gender ? '女' : '男'"
        @click="showPopup = true"
      />

      <van-cell
        is-link
        title="生日"
        :value="userInfo.birthday"
        @click="
          showDatePick = true
          currrentDate = new Date(userInfo.birthday)
        "
      />
    </van-cell-group>

    <!-- 编辑姓名弹窗 -->
    <van-dialog
      v-model="showDialog"
      title="编辑姓名"
      show-cancel-button
      @confirm="updateNickname"
    >
      <van-field ref="input" v-model="newName" placeholder="请输入姓名" />
    </van-dialog>

    <!-- 底部性别弹窗 -->
    <van-popup v-model="showPopup" position="bottom" :style="{ height: '28%' }">
      <van-nav-bar title="性别" left-arrow @click="showPopup = false" />
      <van-cell title="男" is-link @click="updateGender(0)" />
      <van-cell title="女" is-link @click="updateGender(1)" />
    </van-popup>

    <!-- 生日年月日 -->
    <van-popup v-model="showDatePick" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="updateBirthday"
        @cancel="showDatePick = false"
      />
    </van-popup>

    <!-- 图片裁切 -->
    <div class="croppper" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropWidth="180"
        autoCropHeight="180"
      ></vueCropper>
      <van-button type="primary" @click="showCropper = false">取 消</van-button>
      <van-button type="info" @click="updatePhoto">确 认</van-button>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { editUserInfo } from './user.js'
import moment from 'moment'
import { editUserPhoto } from './user.js'
import { VueCropper } from 'vue-cropper'
export default {
  name: 'edit',
  components: {
    VueCropper
  },
  data () {
    return {
      showDialog: false,
      newName: '',
      showPopup: false,
      showDatePick: false,
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(2021, 8, 6),
      currentDate: new Date(),
      showCropper: false,
      img: ''
    }
  },
  methods: {
    editNameClick () {
      this.showDialog = true
      this.newName = this.userInfo.name
      this.$nextTick(() => {
        this.$refs.input.focus()
      })
    },
    updateNickname () {
      editUserInfo({ name: this.newName }).then(res => {
        // console.log(res)
        this.$toast.success('更新成功!')
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          name: this.newName
        })
      })
    },
    updateGender (gender) {
      editUserInfo({ gender }).then(res => {
        // 弹出提示
        this.$toast.success('更新成功')
        // 隐藏popup弹出
        this.showPopup = false
        // 更新vuex
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          gender
        })
      })
    },
    updateBirthday () {
      let birthday = moment(this.currentDate).format('YYYY-MM-DD')
      editUserInfo({ birthday }).then(res => {
        //(1)弹窗提示
        this.$toast.success('更新成功')
        //(2)更新vuex数据
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          birthday
        })
        this.showDatePick = false
      })
    },
    afterRead (file) {
      // 此时可以自行将文件上传至服务器
      // console.log(file)

      this.img = file.content

      // 显示裁切盒子
      this.showCropper = true
    },
    updatePhoto () {
      // 获取截图的blob数据
      this.$refs.cropper.getCropBlob(data => {
        // do something
        // console.log(data)
        let fd = new FormData()
        fd.append('photo', data)
        // 提交文件到服务器
        editUserPhoto(fd).then(res => {
          // console.log(res);
          this.$toast.success('更新成功')
          this.showCropper = false
          // 更新vuex
          this.$store.commit('setUserInfo', {
            ...this.userInfo,
            photo: res.data.data.photo
          })
        })
      })
    }
  },
  computed: {
    ...mapState(['userInfo'])
  }
}
</script>

<style lang="less">
.edit-container {
  // 导航条部分
  .van-nav-bar {
    background-color: #3196fa;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
  }
  // 头像部分
  .avatar {
    display: flex;
    height: 150px;
    justify-content: center;
    align-items: center;
    .van-image {
      // margin-left: 100px;
      width: 120px;
      height: 120px;
    } //文件上传
    .van-uploader {
      opacity: 0;
      position: absolute;
      width: 125px;
      height: 120px;
      border-radius: 50%;
      // transform: translateX(-50%);
      .van-uploader__upload {
        // background-color: red;
        border-radius: 50%;

        transform: translateX(5px);
        width: 120px;
        height: 120px;
      }
    }
  }
  .croppper {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    .van-button--primary {
      position: absolute;
      left: 0;
      bottom: 0;
      width: 40%;
    }
    .van-button--info {
      position: absolute;
      right: 0;
      bottom: 0;
      width: 40%;
    }
  }
}
</style>
